package com.jiaming.employee.department.controller;

import com.jiaming.employee.common.bean.vo.MyPageVO;
import com.jiaming.employee.common.exception.ApiException;
import com.jiaming.employee.common.response.ResultCodeEnum;
import com.jiaming.employee.common.response.ResultVO;
import com.jiaming.employee.department.bean.pojo.Department;
import com.jiaming.employee.department.service.IDepartmentService;
import com.jiaming.employee.salary.bean.vo.GetSalaryPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author dragon
 * @create 2021-05-15 11:21
 */
@Api(value = "部门API接口文档")
@RestController
@RequestMapping("/dep")
public class DepartmentController {

    @Autowired
    IDepartmentService departmentService;

    @ApiOperation(value = "分页查询部门接口",notes = "接口描述")
    @PostMapping("/page_dep")
    public ResultVO<MyPageVO<Department>> pageDep(@RequestBody GetSalaryPageVO GetSalaryPageVO){
        if (GetSalaryPageVO.getPageNum() == null || GetSalaryPageVO.getPageNum() <= 0) {
            GetSalaryPageVO.setPageNum(1);
        }
        if (GetSalaryPageVO.getPageSize() == null || GetSalaryPageVO.getPageSize() < 5) {
            GetSalaryPageVO.setPageSize(5);
        }
        MyPageVO<Department> list = departmentService.pageDmp(GetSalaryPageVO);
        return new ResultVO<>(ResultCodeEnum.SUCCESS,list);
    }

    @ApiOperation(value = "新增部门接口",notes = "接口描述")
    @PostMapping("/save_dep")
    public ResultVO addDep(@RequestBody @Valid Department department){
        departmentService.save(department);
        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "修改部门接口",notes = "接口描述")
    @PostMapping("/update_dep")
    public ResultVO updateDep(@RequestBody Department department){
        if (department.getId() == null || department.getId() <= 0) {
            throw new ApiException("部门id不能为空");
        }
        departmentService.updateById(department);
        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "删除部门接口",notes = "接口描述")
    @PostMapping("/remove_Dep")
    public ResultVO removeDep(@RequestParam Integer id){
        if (id == null || id <= 0) {
            throw new ApiException("缺少必填参数");
        }
        departmentService.removeById(id);
        return new ResultVO(ResultCodeEnum.SUCCESS);
    }
}
